package com.bootdo.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.system.domain.TbOrder;
import com.bootdo.system.domain.TbOrderExample;
import com.bootdo.system.domain.TbOrderExample.Criteria;
import com.bootdo.system.domain.TbViporder;
import com.bootdo.system.domain.TbViporderExample;
import com.bootdo.system.domain.TbWxuser;
import com.bootdo.system.domain.TbWxuserExample;
import com.bootdo.system.mapper.TbOrderMapper;
import com.bootdo.system.mapper.TbViporderMapper;
import com.bootdo.system.mapper.TbWxuserMapper;
import com.bootdo.system.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService{

	
	@Autowired
    private TbOrderMapper om;
	
	@Autowired
    private TbWxuserMapper um;
	
	@Autowired
    private TbViporderMapper vm;
	
	@Override
	public List<TbOrder> list(Map<String, Object> map) {
		return om.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return om.count(map);
	}

	@Override
	public int insert2update(TbOrder table) {
		int res=0;
		if(table != null) {
			Integer id = table.getId();
			if(id != null) {
				res=om.updateByPrimaryKeySelective(table);
			}else {
				res=om.insertSelective(table);
			}
		}
		return res;
	}

	@Override
	public int remove(Integer id) {
		return om.deleteByPrimaryKey(id);
	}

	@Override
	public int batchremove(Integer[] ids) {
		int res=0;
		if(ids.length > 0) {
			for (int i = 0; i < ids.length; i++) {
				res=om.deleteByPrimaryKey(ids[i]);
			}
		}
		return res;
	}

	@Override
	public TbOrder getById(Integer id) {
		return om.selectByPrimaryKey(id);
	}

	@Override
	public List<TbOrder> listx(String openId) {
		return om.listx(openId);
	}

	@Override
	public int insert2updateSvip(TbViporder table) {
		int res=0;
		if(table != null) {
			Integer id = table.getId();
			if(id != null) {
				res=vm.updateByPrimaryKeySelective(table);
			}else {
				table.setPaytime(DateUtils.format(new Date()));
				res=vm.insertSelective(table);
			}
		}
		return res;
	}

	@Override
	public TbViporder getByOrdId(String id) {
		TbViporderExample tbViporderExample = new TbViporderExample();
		com.bootdo.system.domain.TbViporderExample.Criteria createCriteria = tbViporderExample.createCriteria();
		if(id != null) {
			createCriteria.andOidEqualTo(id);
			List<TbViporder> selectByExample = vm.selectByExample(tbViporderExample);
			if(selectByExample.size() > 0) {
				return selectByExample.get(0);
			}
		}
		
		return null;
	}

	@Override
	public List<TbViporder> listKm(Query query) {
		return vm.listKm(query);
	}

	@Override
	public int countKm(Query query) {
		return vm.countKm(query);
	}

	@Override
	public int removeKm(Integer id) {
		return vm.deleteByPrimaryKey(id);
	}

	@Override
	public int batchremoveKm(Integer[] ids) {
		int res=0;
		if(ids.length > 0) {
			for (int i = 0; i < ids.length; i++) {
				res=vm.deleteByPrimaryKey(ids[i]);
			}
		}
		return res;
	}

	@Override
	public List<TbOrder> listx2djs(String openId) {
		return om.listx2djs(openId);
	}

	@Override
	public List<TbOrder> listx2(String openId) {
		//获取自己的订单
		List<TbOrder> listx = om.listx(openId);
//		TbWxuser selectByWxOpenId = selectByWxOpenId(openId);
//		if(selectByWxOpenId != null) {
//			//获取下级订单
//			List<TbWxuser> userByOpenId3 = um.getUserByOpenId4(selectByWxOpenId.getId());
//			for (TbWxuser tbWxuser : userByOpenId3) {
//				String openidXcx = tbWxuser.getOpenidXcx();
//				List<TbOrder> listx2 = om.listx(openidXcx);
//				listx.addAll(listx2);
//				//获取三级
//				List<TbWxuser> userByOpenId4 = um.getUserByOpenId4(tbWxuser.getId());
//				for (TbWxuser tbWxuser2 : userByOpenId4) {
//					String openidXcx2 = tbWxuser2.getOpenidXcx();
//					List<TbOrder> listx3 = om.listx(openidXcx2);
//					listx.addAll(listx3);
//				}
//			}
//		}
		return listx;
	}
	
	public TbWxuser selectByWxOpenId(String openId) {
		TbWxuserExample example = new TbWxuserExample();
		TbWxuserExample.Criteria criteria = example.createCriteria();
		criteria.andOpenidXcxEqualTo(openId);
		List<TbWxuser> tbWxusers = um.selectByExample(example);
		if(tbWxusers == null || tbWxusers.isEmpty()){
			return null;
		}
		return tbWxusers.get(0);
	}


	@Override
	public TbOrder getByIdOid(String id,String openId) {
		TbOrderExample tbOrderExample = new TbOrderExample();
		Criteria createCriteria = tbOrderExample.createCriteria();
		if(StringUtils.isNoneBlank(id) && StringUtils.isNoneBlank(openId)) {
			createCriteria.andOidEqualTo(id);
			createCriteria.andOpenidEqualTo(openId);
		}
		List<TbOrder> selectByExample = om.selectByExample(tbOrderExample);
		if(selectByExample != null && selectByExample.size() > 0) {
			return selectByExample.get(0);
		}
		return null;
	}

	@Override
	public String getDirect2Order(Integer userId, String paytime) {
		return om.getDirect2Order(userId,paytime);
	}

	@Override
	public String getDirect2VipOrder(Integer userId, String paytime) {
		return vm.getDirect2VipOrder(userId,paytime);
	}

	@Override
	public String getProfit2Order(Integer userId, String paytime) {
		return om.getProfit2Order(userId,paytime);
	}

	@Override
	public int getDirect2OrderNum(Integer userId, String paytime) {
		return om.getDirect2OrderNum(userId,paytime);
	}

	@Override
	public int getDirect2VipOrderNum(Integer userId, String paytime) {
		return vm.getDirect2VipOrderNum(userId,paytime);
	}

	@Override
	public List<TbOrder>  getByIdOid2(String id) {
		TbOrderExample tbOrderExample = new TbOrderExample();
		Criteria createCriteria = tbOrderExample.createCriteria();
		createCriteria.andOidEqualTo(id);
		return om.selectByExample(tbOrderExample);
	}

	@Override
	public List<TbOrder> list2(Map<String, Object> map) {
		return om.list2(map);
	}

	@Override
	public int count2(Map<String, Object> map) {
		return om.count2(map);
	}

	@Override
	public int count3(Map<String, Object> map) {
		return om.count3(map);
	}

	@Override
	public TbOrder list2Ceshi(String oid) {
		TbOrderExample tbOrderExample = new TbOrderExample();
		Criteria createCriteria = tbOrderExample.createCriteria();
		createCriteria.andOidEqualTo(oid);
		createCriteria.andCjgxEqualTo(0);
		List<TbOrder> selectByExample = om.selectByExample(tbOrderExample);
		if(selectByExample != null && selectByExample.size() > 0) {
			return selectByExample.get(0);
		}
		return null;
	}

	@Override
	public String getProfit2Order2(Integer userId, String paytime) {
		return om.getProfit2Order2(userId,paytime);
	}

	@Override
	public List<Map<String, Object>> list5(Map<String, Object> map) {
		return om.list5(map);
	}

	@Override
	public int count5(Map<String, Object> map) {
		return om.count5(map);
	}

	@Override
	public List<Map<String, Object>> list6(Map<String, Object> map) {
		return om.list6(map);
	}

	@Override
	public int count6(Map<String, Object> map) {
		return om.count6(map);
	}

	@Override
	public String getProfit2Order3(String openId, String paytime) {
		return om.getProfit2Order3(openId,paytime);
	}

	@Override
	public int getDirect2OrderNum2(String openId, String paytime) {
		return om.getDirect2OrderNum2(openId,paytime);
	}

	@Override
	public int getDirect2VipOrderNum2(String openId, String paytime) {
		return vm.getDirect2VipOrderNum2(openId,paytime);
	}

	@Override
	public Map<String, Object> listx5(String openId) {
		Map<String,Object> map=new HashMap<String, Object>();
		List<TbOrder> listx = om.listx5(openId,2);
		List<TbOrder> yfk = om.listx5(openId,1);
		List<TbOrder> ytk = om.listx5(openId,0);
		map.put("ywc", listx);
		map.put("yfk", yfk);
		map.put("ytk", ytk);
		return map;
	}

}
